﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IsmsExplorer.Model.Iso27001 {
    public class Iso27001Control : Iso27001ControlBase {
        #region Data
        private string _description;
        private Iso27001ControlSubsection _subsection;
        #endregion

        #region Construction
        public Iso27001Control(int id, string referenceID, string name, string description, Iso27001ControlSubsection subsection) : base(id, referenceID, name) 
        {
            this.Description = description;
            this.Subsection = subsection;
        }

        public Iso27001Control(int id, string referenceID, string name, string description) : this(id, referenceID, name, description, null) 
        {
        }
        #endregion

        public string Description {
            get { return _description; }
            protected set { _description = value; }
        }

        public Iso27001ControlSubsection Subsection {
            get { return _subsection; }
            set { _subsection = value; }
        }

        #region Equality tests
        /// <summary>
        /// Equality based on two controls having same type and same reference ID!
        /// NOTE: could only check the database ID as things stand now but it's safer with reference ID.
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Equals(object obj) {
            if (obj == null || !(obj is Iso27001Control)) {
                return false;
            }
            return (this == (Iso27001Control)obj);
        }

        /// <summary>
        /// Equality based on two controls having same type and same reference ID!
        /// NOTE: could only check the database ID as things stand now but it's safer with reference ID.
        /// </summary>
        /// <param name="l"></param>
        /// <param name="r"></param>
        /// <returns></returns>
        public static bool operator ==(Iso27001Control l, Iso27001Control r) {
            // NOTE: need the cast, otherwise infinite loop!
            if (((object)l == null) && ((object)r == null)) {
                return true;
            }

            if (((object)l == null) || ((object)r == null)) {
                return false;
            }

            if (l.GetType() != r.GetType()) {
                return false;
            }

            if (l.ReferenceID != r.ReferenceID) {
                return false;
            }

            return true;
        }

        /// <summary>
        /// Equality based on two controls having same type and same reference ID!
        /// NOTE: could only check the database ID as things stand now but it's safer with reference ID.
        /// </summary>
        /// <param name="l"></param>
        /// <param name="r"></param>
        /// <returns></returns>
        public static bool operator !=(Iso27001Control l, Iso27001Control r) {
            return !(l == r);
        }

        /// <summary>
        /// Uses the hashcode of the reference ID (string).
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode() {
            return this.ReferenceID.GetHashCode();
        }

        #endregion

    } // end public class Iso27001Control : Iso27001ControlBase
}
